﻿namespace MDXStudio
{
    using AdvancedDataGridView;
    using Microsoft.AnalysisServices;
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;

    internal class TraceWatcher
    {
        public Server m_AMO;
        private TWEvent m_CurrentEvent;
        private TWQuery m_CurrentQuery;
        private TWSEQueryData m_CurrentSEQuery;
        private List<TWEvent> m_Events = new List<TWEvent>();
        private Queue<string> m_QueryDescriptions = new Queue<string>();
        private AdvancedDataGridView.TreeGridView m_TreeGridView;

        internal TraceWatcher()
        {
        }

        internal void AddWatchQuery(string description)
        {
            this.m_QueryDescriptions.Enqueue(description);
        }

        internal void OnExecuteMdxScriptBegin(TraceEventArgs e)
        {
            if (this.m_CurrentEvent != null)
            {
                TWExecuteMdxScript script = new TWExecuteMdxScript(e);
                this.m_CurrentEvent.Add(script);
                this.SetCurrentEvent(script);
            }
        }

        internal void OnExecuteMdxScriptEnd(TraceEventArgs e)
        {
            while ((this.m_CurrentEvent != null) && (this.m_CurrentEvent.GetType() != typeof(TWExecuteMdxScript)))
            {
                this.ResetCurrentEvent();
            }
            this.m_CurrentEvent.Duration = e.get_Duration();
            this.ResetCurrentEvent();
        }

        internal void OnNonEmptyBegin(TraceEventArgs e)
        {
            if (this.m_CurrentEvent != null)
            {
                if (this.m_CurrentEvent.GetType() == typeof(TWCalculateAxes))
                {
                    this.ResetCurrentEvent();
                }
                TWNonEmpty empty = new TWNonEmpty(e);
                this.m_CurrentEvent.Add(empty);
                this.SetCurrentEvent(empty);
            }
        }

        internal void OnNonEmptyEnd(TraceEventArgs e)
        {
            while ((this.m_CurrentEvent != null) && (this.m_CurrentEvent.GetType() != typeof(TWNonEmpty)))
            {
                this.ResetCurrentEvent();
            }
            if (this.m_CurrentEvent != null)
            {
                this.m_CurrentEvent.Duration = e.get_Duration();
            }
            this.ResetCurrentEvent();
        }

        internal void OnPartitionQuery(TraceEventArgs e)
        {
            if (this.m_CurrentSEQuery == null)
            {
                this.m_CurrentSEQuery = new TWSEQueryData(e);
            }
            TWEvent event2 = null;
            if (e.get_TextData().EndsWith("aggregation."))
            {
                TWAggregation aggregation = new TWAggregation(e);
                aggregation.AggregationName = e.get_ObjectName();
                event2 = aggregation;
            }
            else
            {
                TWPartition partition = new TWPartition(e);
                partition.PartitionName = e.get_ObjectName();
                event2 = partition;
            }
            this.m_CurrentSEQuery.Add(event2);
        }

        internal void OnQueryBegin(TraceEventArgs e)
        {
            if (this.m_QueryDescriptions.Count == 0)
            {
                this.m_CurrentQuery = null;
            }
            else
            {
                string desc = this.m_QueryDescriptions.Dequeue();
                TWQuery item = new TWQuery(e, desc);
                this.m_Events.Add(item);
                this.SetCurrentEvent(item);
                this.m_CurrentQuery = item;
                if (e.get_EventSubclass() == 0x2d)
                {
                    TWCalculateAxes axes = new TWCalculateAxes(e);
                    this.m_CurrentQuery.Add(axes);
                    this.SetCurrentEvent(axes);
                }
            }
        }

        internal void OnQueryEnd(TraceEventArgs e)
        {
            if (this.m_CurrentQuery != null)
            {
                this.m_CurrentQuery.Duration = e.get_Duration();
                this.ResetCurrentEvent();
                VisualizeTrace method = new VisualizeTrace(this.Visualize);
                this.m_TreeGridView.Invoke(method, new object[] { this.m_TreeGridView });
            }
        }

        internal void OnQuerySubcubeExEnd(TraceEventArgs e)
        {
            if (this.m_CurrentEvent != null)
            {
                string str = null;
                string[] strArray = e.get_ObjectPath().Split(new char[] { '.' });
                if (this.m_AMO != null)
                {
                    int length = strArray.Length;
                    if (length >= 3)
                    {
                        str = this.m_AMO.get_Databases().get_Item(strArray[length - 3]).get_Cubes().get_Item(strArray[length - 2]).get_MeasureGroups().get_Item(strArray[length - 1]).get_Name();
                    }
                }
                if (e.get_EventSubclass() == 150)
                {
                    if (this.m_CurrentSEQuery == null)
                    {
                        this.m_CurrentSEQuery = new TWSEQueryData(e);
                    }
                    else
                    {
                        this.m_CurrentSEQuery.m_TraceEventArgs = e;
                    }
                    this.m_CurrentSEQuery.m_MeasureGroupName = str;
                    this.m_CurrentSEQuery.Duration = e.get_Duration();
                    this.m_CurrentEvent.Add(this.m_CurrentSEQuery);
                    this.m_CurrentSEQuery = null;
                }
                else
                {
                    TWCacheData data = new TWCacheData(e);
                    data.m_MeasureGroupName = str;
                    this.m_CurrentEvent.Add(data);
                }
            }
        }

        internal void OnSerializeBegin(TraceEventArgs e)
        {
            if (this.m_CurrentEvent != null)
            {
                if (this.m_CurrentEvent.GetType() == typeof(TWCalculateAxes))
                {
                    this.ResetCurrentEvent();
                }
                TWSerialize serialize = new TWSerialize(e);
                this.m_CurrentEvent.Add(serialize);
                this.SetCurrentEvent(serialize);
            }
        }

        internal void OnSerializeEnd(TraceEventArgs e)
        {
            if (this.m_CurrentEvent != null)
            {
                while (this.m_CurrentEvent.GetType() != typeof(TWSerialize))
                {
                    this.ResetCurrentEvent();
                }
                this.m_CurrentEvent.Duration = e.get_Duration();
                this.ResetCurrentEvent();
            }
        }

        private void ResetCurrentEvent()
        {
            if (this.m_CurrentEvent != null)
            {
                this.m_CurrentEvent = this.m_CurrentEvent.m_Parent;
            }
        }

        private void SetCurrentEvent(TWEvent e)
        {
            this.m_CurrentEvent = e;
        }

        internal void Visualize(AdvancedDataGridView.TreeGridView treeGridView)
        {
            if (this.m_CurrentQuery != null)
            {
                long duration = this.m_CurrentQuery.Duration;
                this.m_CurrentQuery.Regroup();
                this.m_CurrentQuery.UpdateRatios(duration, duration);
                TreeGridNode root = treeGridView.Nodes.Add(new object[] { this.m_CurrentQuery.GetLabel(), this.m_CurrentQuery.Duration.ToString(), "", "" });
                root.ImageIndex = 1;
                foreach (TWEvent event2 in this.m_CurrentQuery.m_SubEvents)
                {
                    event2.Visualize(root);
                }
                root.Expand();
            }
        }

        internal AdvancedDataGridView.TreeGridView TreeGridView
        {
            set
            {
                this.m_TreeGridView = value;
            }
        }

        public delegate void VisualizeTrace(TreeGridView treeGridView);
    }
}

